package com.example.lanouhn.bantangapp.activities.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.yongchun.library.view.ImageSelectorActivity;


/**
 * Created by lanouhn on 16/7/21.
 */
public class PublishActivity extends Activity {
    private boolean isVisible;//定义是否隐藏  默认是
    private LinearLayout include; //隐藏布局
    private EditText et_zw;    // ed控件
    private EditText et_title;//标题
    private ImageView back;
    private TextView yl;//预览
    private TextView fabu;//发布
    private boolean isEmpty = true;

    //隐藏布局的控件
    private RelativeLayout pic;
    private RelativeLayout shopping;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.publish_activity);
        include = (LinearLayout) findViewById(R.id.include);//找到隐藏控件
        et_zw = (EditText) findViewById(R.id.et_zw);
        et_title = (EditText) findViewById(R.id.et_title);
        back = (ImageView) findViewById(R.id.back);
        yl = (TextView) findViewById(R.id.yl);
        fabu = (TextView) findViewById(R.id.fabu);
        pic= (RelativeLayout) findViewById(R.id.pic);
        shopping= (RelativeLayout) findViewById(R.id.shopping);
        initLinstener();//点击事件
    }

    //点击显示隐藏
    private void initLinstener() {
        et_zw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isVisible = false;
                if (isVisible == false) {
                    include.setVisibility(View.VISIBLE);
                    isVisible = true;//这一句显示隐藏布局区域
                }
            }

        });
        //滑动隐藏消失
        et_zw.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (isVisible == true) {
                    include.setVisibility(View.INVISIBLE);
                    isVisible = false;
                }
                return false;
            }
        });
        //跳转回首页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               finish();
            }
        });


      // 监听标题输入框和正文输入框不为空的时候设置预览和发布按钮点击的判断
        et_zw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(et_title.getText().toString().trim().length()>0&&et_zw.getText().toString().trim().length()>0){
                    fabu.setTextColor(getResources().getColor(R.color.colorAccent));
                    yl.setTextColor(getResources().getColor(R.color.blank));
                    //预览点击事件
                    yl.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(PublishActivity.this, R.string.PuT, Toast.LENGTH_SHORT).show();
                        }
                    });
                    //发布点击事件
                    fabu.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Toast.makeText(PublishActivity.this, R.string.PuT, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{
                    fabu.setTextColor(getResources().getColor(R.color.blank));
                    yl.setTextColor(getResources().getColor(R.color.gray));
                    fabu.setClickable(false);//设置发布按钮不能点击
                    yl.setClickable(false);//设置预览按钮不能点击
                }
            }
            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        // 监听正文输入框和正文输入框不为空的时候设置预览和发布按钮点击的判断
        et_title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if(et_title.getText().toString().trim().length()>0&&et_zw.getText().toString().trim().length()>0){
                    fabu.setTextColor(getResources().getColor(R.color.colorAccent));
                    yl.setTextColor(getResources().getColor(R.color.blank));
                    fabu.setClickable(true);//设置发布按钮可以点击
                    yl.setClickable(true);//设置预览按钮可以点击
                }else{
                    fabu.setTextColor(getResources().getColor(R.color.blank));
                    yl.setTextColor(getResources().getColor(R.color.gray));
                    fabu.setClickable(false);//设置发布按钮不能点击
                    yl.setClickable(false);//设置预览按钮不能点击
                }
   }
            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        //图片的监听点击事件
        pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ImageSelectorActivity.start(PublishActivity.this, 9, ImageSelectorActivity.MODE_SINGLE, true, true, true);
            }
        });
        //商品的点击事件
        shopping.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(PublishActivity.this,PublishCommodityActivity.class);
                startActivity(it);
                finish();
            }
        });
    }
}
